﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using WCF.Practice.CustomerHost.Service;

namespace WCF.Practice.CustomerHost.Host
{
    class Program
    {
        //static void Main(string[] args)
        //{
        //    var host = new ServiceHost(typeof (UserService));

        //    host.Open();

        //    Console.WriteLine("Service started");

        //    Console.ReadLine();

        //    host.Close();
        //}

        static void Main(string[] args)
        {
            var host = new ServiceHost(typeof(UserService));

            ConfigureEndpoint(host);

            ConfigureBehavior(host);

            host.Open();

            Console.WriteLine("Service started");

            Console.ReadLine();

            host.Close();
        }

        private static void ConfigureEndpoint(ServiceHost host)
        {
            var address = "net.tcp://localhost:8009/UserService";
            var binding = new NetTcpBinding();
            binding.MaxConnections = 10;
            binding.SendTimeout = TimeSpan.FromMinutes(3);
            binding.MaxReceivedMessageSize = 20000000;

            var contract = typeof (IUserService);

            host.AddServiceEndpoint(contract, binding, address);
        }

        private static void ConfigureBehavior(ServiceHost host)
        {
            var behavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();
            if (behavior == null)
            {
                behavior = new ServiceDebugBehavior();
                behavior.IncludeExceptionDetailInFaults = true;
                host.Description.Behaviors.Add(behavior);
            }
            else
            {
                behavior.IncludeExceptionDetailInFaults = true;
            }
        }
    }
}
